package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;

import com.mysema.query.BooleanBuilder;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AftersaleTransferRecordService{

	@Autowired
	private AftersaleTransferRecordDao aftersaleTransferRecordDao;
	
	private QAftersaleTransferRecord $ = QAftersaleTransferRecord.aftersaleTransferRecord;
	
	@Autowired
	public void setAftersaleTransferRecordDao(AftersaleTransferRecordDao aftersaleTransferRecordDao) {
		this.aftersaleTransferRecordDao = aftersaleTransferRecordDao;
	}
	
	public List<AftersaleTransferRecord> findAll(){
		return ListUtil.toList(aftersaleTransferRecordDao.findAll());
	}
				
	public List<AftersaleTransferRecord> findAll(
	Long originalHandlePersonId,
	Long targetHandlePersonId,
	Long aftersaleRecordId,
	Date transferTimeBegin,
	Date transferTimeEnd,
	String remark,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(IdUtil.isNotEmpty(originalHandlePersonId)){
		condition.and($.originalHandlePerson.id.eq(originalHandlePersonId));
	}
	
	if(IdUtil.isNotEmpty(targetHandlePersonId)){
		condition.and($.targetHandlePerson.id.eq(targetHandlePersonId));
	}
	
	if(IdUtil.isNotEmpty(aftersaleRecordId)){
		condition.and($.aftersaleRecord.id.eq(aftersaleRecordId));
	}
	
	if(transferTimeBegin != null){
		condition.and($.transferTime.after(transferTimeBegin));
	}
	
	if(transferTimeEnd != null){
		condition.and($.transferTime.before(transferTimeEnd));
	}
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	
	return ListUtil.toList(aftersaleTransferRecordDao.findAll(condition));
	}		

	public Page<AftersaleTransferRecord> findAll(
			Long originalHandlePersonId,
			Long targetHandlePersonId,
			Long aftersaleRecordId,
			Date transferTimeBegin,
			Date transferTimeEnd,
			String remark,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
	
			if(IdUtil.isNotEmpty(originalHandlePersonId)){
				condition.and($.originalHandlePerson.id.eq(originalHandlePersonId));
			}
			if(IdUtil.isNotEmpty(targetHandlePersonId)){
				condition.and($.targetHandlePerson.id.eq(targetHandlePersonId));
			}
			if(IdUtil.isNotEmpty(aftersaleRecordId)){
				condition.and($.aftersaleRecord.id.eq(aftersaleRecordId));
			}
			if(transferTimeBegin != null){
				condition.and($.transferTime.after(transferTimeBegin));
			}
			
			if(transferTimeEnd != null){
				condition.and($.transferTime.before(transferTimeEnd));
			}
			if(StringUtil.isNotEmpty(remark)){
				condition.and($.remark.contains(remark));
			}
	
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject);
			return aftersaleTransferRecordDao.findAll(condition, pageRequest);
	}
	
	
	
	public AftersaleTransferRecord getAftersaleTransferRecord(Long id){
		return aftersaleTransferRecordDao.getOne(id);
	}
	
	public void deleteAftersaleTransferRecord(Long id){
		aftersaleTransferRecordDao.delete(id);
	}

	public void saveAftersaleTransferRecord(AftersaleTransferRecord aftersaleTransferRecord) {
		aftersaleTransferRecordDao.save(aftersaleTransferRecord);
	}

}
		
